package com.hlyj.http.ad

import android.app.Activity
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTFeedAd
import com.bytedance.sdk.openadsdk.mediation.ad.MediationExpressRenderListener
import com.hnyyac.ad.mds.GMAds
import com.hnyyac.ad.mds.utils.BaseAdConstants
import com.hnyyac.ad.mds.utils.BaseAdSpUtils

import com.hnyyac.ad.mds.utils.UIUtils

class NewGroMoreFeedAdManager(
    private val activity: Activity,
    private val adUnitId: String,
    private val adContainer: FrameLayout?,
    private val position:Int


) {
    companion object {
        private const val TAG = "qin"
    }
    private var width: Int = 0
    private var height: Int = 0
    private var ttFeedAd: TTFeedAd? = null

    // 广告监听器
    private val feedAdListener = object : TTAdNative.FeedAdListener {
        override fun onError(code: Int, message: String?) {
            Log.e(TAG, "广告加载失败: $code, $message")
        }

        override fun onFeedAdLoad(ads: MutableList<TTFeedAd>?) {
            if (ads.isNullOrEmpty()) {
                Log.e(TAG, "广告加载成功但返回空列表")
                return
            }
            ttFeedAd = ads[0]
            GMAds.feedListener?.onLoad(adUnitId)
            showAd()
           // Log.d(TAG, "广告加载成功")
        }
    }

    // 模板广告渲染监听器
    private val expressRenderListener = object : MediationExpressRenderListener {
        override fun onRenderFail(view: View?, message: String?, code: Int) {
            Log.e(TAG, "广告渲染失败: $code, $message")
            GMAds.feedListener?.onError(message?: "广告渲染失败",adUnitId)
        }

        override fun onAdClick() {
            ttFeedAd?.let { GMAds.feedListener?.onAdClick(it, adUnitId) }
        }

        override fun onAdShow() {
            if (ttFeedAd != null){
               // Log.e(TAG, "不是空")
                ttFeedAd?.let { GMAds.feedListener?.onAdShow(it, adUnitId,position) }
            }else{
                Log.e(TAG, "ttFeedAd是空的")
            }
        }

        override fun onRenderSuccess(
            view: View?,
            width: Float,
            height: Float,
            isAutoPlay: Boolean
        ) {
           // Log.d(TAG, "广告渲染成功")
            ttFeedAd?.adView?.let { adView ->
                adContainer?.apply {
                    removeAllViews()
                    addView(adView)
                }
            }
        }
    }

    /**
     * 加载Feed广告
     */
    fun loadAd() {
        if (BaseAdSpUtils.getInstance().getInt(BaseAdConstants.ISVIP)==1)return//如果有会员不执行
       // Log.e("qin","传进来的宽高"+width+"---"+height)
        // 1. 创建广告位参数
        val adSlot = AdSlot.Builder()
            .setCodeId(adUnitId)
            .setExpressViewAcceptedSize(
                (if (width == 0) UIUtils.getScreenWidthDp(activity) else width).toFloat(),
                0.toFloat(), // 默认高度 340dp,

            )
            .setAdCount(1)
            .build()

        // 2. 获取TTAdNative实例
        val adNative = TTAdSdk.getAdManager().createAdNative(activity)

        // 3. 加载广告
        GMAds.feedListener?.onTrigger(adUnitId)
        adNative.loadFeedAd(adSlot, feedAdListener)
    }

    /**
     * 展示已加载的广告
     */
    fun showAd() {
        ttFeedAd ?: run {
            Log.e(TAG, "请先加载广告或等待广告加载完毕")
            return
        }

        ttFeedAd?.mediationManager?.let { manager ->
            if (manager.isExpress) {
                // 模板广告
                ttFeedAd?.setExpressRenderListener(expressRenderListener)
                ttFeedAd?.render()
            } else {
                Log.e(TAG, "广告未准备好或不是模板广告")
            }
        } ?: Log.e(TAG, "mediationManager为空")
    }

    /**
     * 销毁广告
     */
    fun destroy() {
        ttFeedAd?.destroy()
        ttFeedAd = null
    }

    fun setFeedAdWidth(width: Int,height: Int) {
        this.width = width
        this.height = height
    }
}